﻿using Common.EF.Structs;
using Common.EF.Utils;
using Common.Infrastructure;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.EF
{
    public class CommonContextPoolParam : ICommonContextPoolParam
    {
        private readonly IServiceProvider provider;
        private readonly IConfiguration configuration;

        private readonly List<CommonContextParam> contexts = null;

        public CommonContextPoolParam(IServiceProvider provider, IConfiguration configuration)
        {
            this.provider = provider;
            this.configuration = configuration;
            this.contexts = this.DoGetAccess();
        }

        public List<CommonContextParam> Contexts
        {
            get { return contexts; }
        } 

        private List<CommonContextParam> DoGetAccess()
        {
            var s = CommonContextParamCache.Params;
            if (s == null)
            {
                s = ContextInitUtils.GetContextParamConfig(this.configuration);
                CommonContextParamCache.Params = s;
            }

            return s;
        }
    }
}
